package com.febers.ihealth.fragment

import com.febers.ihealth.R
import com.febers.ihealth.base.BaseFragment
import com.febers.ihealth.entity.ReleaseNextSurveyEvent
import com.febers.ihealth.utils.Event
import com.febers.ihealth.utils.log
import com.febers.ihealth.utils.user
import kotlinx.android.synthetic.main.survey_shetou.*

class SurveyShetou: BaseFragment() {

    private var chooseShezhiGroup = false
    private var chooseShezhiButton = false

    private var chooseShetaiButton = false
    private var chooseShetaiGroup = false

    override fun setView(): Int = R.layout.survey_shetou

    override fun initView() {
        //舌质
        radio_group_shezhi.setOnCheckedChangeListener { group, checkedId ->
            user.clearShezhi()
            when(checkedId) {
                R.id.radio_button_shedanbai -> user.shezhibai = true
                R.id.radio_button_shebianjianhong -> user.shebianjianhong = true
                R.id.radio_button_sheanhong -> user.shezhianhong = true
                R.id.radio_button_shezhihong -> user.shezhihong = true
            }
            chooseShezhiGroup = true
            onCheckChange()
        }
        radio_button_shechihen.setOnCheckedChangeListener { buttonView, isChecked ->
            user.shechihen = isChecked
            chooseShezhiButton= isChecked
            onCheckChange()
        }
        radio_group_she_ganhua.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId) {
                R.id.radio_button_shezhigan -> {
                    user.shezhigan = true
                    user.sheshuihua = false
                }
                R.id.radio_button_sheshuihua -> {
                    user.shezhigan = false
                    user.sheshuihua = true
                }
            }
            chooseShezhiGroup = true
            onCheckChange()
        }

        //舌苔
        radio_button_shetaibai.setOnCheckedChangeListener { buttonView, isChecked ->
            user.shetaibai = isChecked
            radio_button_shetaiwu.clearCheck()
            if (!user.shetaihou) {  //另一个没被选择
                chooseShetaiButton = isChecked
            }
            onCheckChange()
        }
        radio_button_shetaihuang.setOnCheckedChangeListener { buttonView, isChecked ->
            user.shetaihou = isChecked
            radio_button_shetaiwu.clearCheck()
            if (!user.shetaibai) {
                chooseShetaiButton = isChecked
            }
            onCheckChange()
        }
        radio_group_shetai_houdu.setOnCheckedChangeListener { group, checkedId ->
            user.clearSheTaihoudu()
            radio_button_shetaiwu.clearCheck()
            when(checkedId) {
                R.id.radio_button_shetaihou -> user.shetaihou = true
                R.id.radio_button_shetaibao -> user.shetaibao = true
                R.id.radio_button_shetaishao -> user.shetaishao = true
            }
            chooseShetaiGroup = true
            onCheckChange()
        }
        radio_button_shetaiwu.setOnCheckedChangeListener { buttonView, isChecked ->
            log {
                "无舌苔被点击: $isChecked"
            }
            if (isChecked) {
                radio_button_shetaibai.clearCheck()
                radio_button_shetaihuang.clearCheck()
                radio_group_shetai_houdu.clearCheck()
                radio_group_shetai_nifu.clearCheck()
            }
            user.shetaiwu = isChecked
            chooseShetaiGroup = false
            chooseShetaiButton = isChecked
            onCheckChange()
        }
        radio_group_shetai_nifu.setOnCheckedChangeListener { group, checkedId ->
            radio_button_shetaiwu.clearCheck()
            when(checkedId) {
                R.id.radio_button_shetaini -> {
                    user.shetaini = true
                    user.shetaifu = false
                }
                R.id.radio_button_shetaifu -> {
                    user.shetaini = false
                    user.shetaifu = true
                }
            }
            chooseShetaiGroup = true
            onCheckChange()
        }
    }

    fun onCheckChange() {
        val chooseShezhi = chooseShezhiButton || chooseShezhiGroup
        val chooseShetai = chooseShetaiButton || chooseShetaiGroup
        if (chooseShezhi && chooseShetai) {
            Event.post(ReleaseNextSurveyEvent(true))
        } else {
            Event.post(ReleaseNextSurveyEvent(false))
        }
    }

    override fun lazyInit() {
    }

    override fun onVisible() {
        Event.post(ReleaseNextSurveyEvent(false))
        onCheckChange()
    }
}